<?php

namespace app\index\controller;

use think\Cache;
use think\Controller;
use think\Db;
use think\Loader;
use think\Request;
use think\Session;
use think\Validate;
use tree\Tree;

class Index extends Controller
{
    public function login()
    {
        $user_id = session('user_id');
        $shop_id = session('shop_id');

        if ($user_id && $shop_id) $this->redirect('/index/index/index');

        return $this->fetch();
    }

    public function logout()
    {
        session('user_id', null);
        session('shop_id', null);

        apiReturn(1, '退出登录成功');
    }

    public function index()
    {
        $user_id = session('user_id');
        $shop_id = session('shop_id');

        $this->assign('user_id', $user_id);
        $this->assign('shop_id', $shop_id);
        return $this->fetch();
    }

    public function addCar()
    {
        $user_id = session('user_id');
        $shop_id = session('shop_id');

        if (!($user_id && $shop_id)) $this->redirect('index/index/login');

        $shop_name = Db::name('shop')->where(['id' => $shop_id])->value('name');
        $this->assign('shop_name', $shop_name);
        return $this->fetch();
    }

    public function carDetail()
    {
        return $this->fetch();
    }

    /**
     * 登录
     * */
    public function accountLogin()
    {
        $input = Request()->param();

        $account = $input['account'];
        $password = $input['password'];

        if (empty($account)) apiReturn(2, '请输入账号');
        if (empty($password)) apiReturn(2, '请输入密码');

        $info = Db::name('user')->where(['account' => $account])->find();
        if (empty($info)) apiReturn(2, '账号不存在');
        if ($info['status'] == 2) apiReturn(2, '账号已被禁用');
        if (thinkEncrypt($password) !== $info['password']) apiReturn(2, '密码错误');
        if (empty($info['shop_id'])) apiReturn(2, '登录失败，账号未绑定门店');

        session('user_id', $info['id']);
        session('shop_id', $info['shop_id']);

        apiReturn(1, '登录成功', url('/index/index/index'));
    }

    /**
     * 新增质保信息录入
     * */
    public function addInformation()
    {
        $input = Request()->param();

        $user_id = session('user_id');
        $shop_id = session('shop_id');
        $license_plate = $input['license_plate'];
        $vin = $input['vin'];
        $car_model = $input['car_model'];
        $year = $input['year'];
        $machine_model = $input['machine_model'];
        $serial_number = $input['serial_number'];
        $customer_name = $input['customer_name'];
        $customer_mobile = $input['customer_mobile'];
        $installation_date = $input['installation_date'];

        if (!($user_id && $shop_id)) apiReturn(3, '登录信息有误，请重新登录', url('index/index/login'));
        /*if (empty($license_plate)) apiReturn(2, '车牌号不能为空');
        if (empty($vin)) apiReturn(2, '车架号不能为空');
        if (!preg_match('/^[A-HJ-NPR-Z0-9]{17}$/', $vin)) apiReturn(2, '车架号格式错误');
        if (empty($car_model)) apiReturn(2, '车型不能为空');
        if (empty($year)) apiReturn(2, '年份不能为空');
        if (empty($machine_model)) apiReturn(2, '机器型号不能为空');
        if (empty($serial_number)) apiReturn(2, '防伪流水号不能为空');
        if (empty($customer_name)) apiReturn(2, '客户姓名不能为空');
        if (empty($customer_mobile)) apiReturn(2, '联系方式不能为空');
        if (!preg_match('/^1[3-9]\d{9}$/', $customer_mobile)) apiReturn(2, '联系方式格式错误');
        if (empty($installation_date)) apiReturn(2, '安装日期不能为空');*/
        if (empty($license_plate) && empty($vin) && empty($serial_number) && empty($customer_mobile)) apiReturn(2, '车牌/车架号/防伪流水号/联系方式至少填写一项');
        if ($vin && !preg_match('/^[A-HJ-NPR-Z0-9]{17}$/', $vin)) apiReturn(2, '车架号格式错误');
        if ($customer_mobile && !preg_match('/^1[3-9]\d{9}$/', $customer_mobile)) apiReturn(2, '联系方式格式错误');

        if ($license_plate) {
            $license_plate_count = Db::name('information')->where(['license_plate' => $license_plate])->count();
            if ($license_plate_count > 0) apiReturn(2, '车牌已存在');
        }
        if ($vin) {
            $vin_count = Db::name('information')->where(['vin' => $vin])->count();
            if ($vin_count > 0) apiReturn(2, '车架号已存在');
        }
        if ($serial_number) {
            $serial_number_count = Db::name('information')->where(['serial_number' => $serial_number])->count();
            if ($serial_number_count > 0) apiReturn(2, '防伪流水号已存在');
        }
        if ($customer_mobile) {
            $customer_mobile_count = Db::name('information')->where(['customer_mobile' => $customer_mobile])->count();
            if ($customer_mobile_count > 0) apiReturn(2, '联系方式已存在');
        }

        if (empty($installation_date)) apiReturn(2, '安装日期不能为空');

        $data = [
            'user_id' => $user_id,
            'shop_id' => $shop_id,
            'license_plate' => $license_plate,
            'vin' => $vin,
            'car_model' => $car_model,
            'year' => $year,
            'machine_model' => $machine_model,
            'serial_number' => $serial_number,
            'customer_name' => $customer_name,
            'customer_mobile' => $customer_mobile,
            'installation_date' => $installation_date,
            'create_time' => time()
        ];

        $res = Db::name('information')->insert($data);
        if ($res) {
            apiReturn(1, '提交成功', url('/index/index/index'));
        } else {
            apiReturn(2, '提交失败');
        }
    }

    /**
     * 查询质保信息录入
     * */
    public function searchInformation()
    {
        $input = Request()->param();

        $search_value = $input['search_value'];
        //        $shop_id = session('shop_id');

        //        if (empty($shop_id)) apiReturn(3, '您还未登录，请先登录');
        if (empty($search_value)) apiReturn(2, '请输入查询条件');

        $list = Db::name('information')
            //            ->where('shop_id', 15)
            ->where(function ($query) use ($search_value) {
                $query->where('license_plate', $search_value)
                    ->whereOr('vin', $search_value)
                    ->whereOr('serial_number', $search_value)
                    ->whereOr('customer_mobile', $search_value);
            })
            ->order('installation_date desc')
            ->select();

        if ($list) {
            foreach ($list as &$v) {
                $v['shop_name'] = getShopName($v['shop_id']);
            }
            apiReturn(1, '获取数据成功', $list);
        } else {
            apiReturn(2, '暂无数据');
        }
    }


}
